package com.class05.service;

import com.class05.dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @ClassDescription:
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/11/7 19:45
 */
@Service
@Transactional(timeout = 3)
public class StudentService {
    @Autowired
    public StudentDao studentDao;

    @Transactional(readOnly = false, rollbackFor = {Exception.class},
            isolation = Isolation.READ_COMMITTED)
    public void changeInfo() throws FileNotFoundException {
        studentDao.updateAgeById(80, 1);
        new FileInputStream("xxxx");
        studentDao.updateNameById("王hfhf", 1);
    }

    @Transactional(readOnly = false, timeoutString = "3")
    public void changeInfo1() {
        studentDao.updateAgeById(90, 1);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        studentDao.updateNameById("王", 1);
    }
    /* 声明两个独立修改数据库的事务业务方法
    propagation = Propagation
    * */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void changeAge() {
        studentDao.updateAgeById(998, 1);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void changeName() {
        studentDao.updateNameById("二狗子", 1);
    }

}
